package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IAppraiseBizService;
import com.kk.xhr.admin.controller.request.AppraiseAddRequest;
import com.kk.xhr.admin.controller.request.AppraiseModifyRequest;
import com.kk.xhr.admin.controller.request.AppraiseQueryRequest;
import com.kk.xhr.admin.controller.vo.AppraiseVO;
import com.kk.xhr.common.pojo.AppraiseResultEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 考评管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "考评管理", tags = "考评管理")
@RequestMapping("/appraise")
public class AppraiseController {
    private final Executor adminControllerBizExecutor;
    private final IAppraiseBizService appraiseBizService;

    @ApiOperation(value = "分页查询考评")
    @RequiresRoles(value = {"admin"})
    @GetMapping
    public CompletionStage<ApiResult<PageVO<AppraiseVO>>> queryPage(@Valid AppraiseQueryRequest request) {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(appraiseBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询我的考评")
    @GetMapping("mine")
    public CompletionStage<ApiResult<List<AppraiseVO>>> queryMine() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> ApiResult.success(appraiseBizService.queryMine(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询部门成员的待考评记录")
    @GetMapping("/member")
    public CompletionStage<ApiResult<List<AppraiseVO>>> queryMemberAppraise() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() -> ApiResult.success(appraiseBizService.queryMemberAppraise(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "批量添加考评")
    @PostMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> batchAdd(@RequestBody @Valid AppraiseAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            appraiseBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改考评")
    @PutMapping("/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody @Valid AppraiseModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            appraiseBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改考评状态")
    @PutMapping("/result/{id:.+}")
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, AppraiseResultEnum result) {
        return CompletableFuture.supplyAsync(() -> {
            appraiseBizService.modifyStatus(id, result);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
